React Testing Library
from Testing Library
docs
Testing LibraryのQuery
追加で入れるlibraryなど
必須なものと、あると便利なものの差が分かりづらい #??
@testing-library/react
「React Testing Library」がこれのこと
testing-library自体はVueなど向けにも提供してる
JSDom
@testing-library/jest-dom
@testing-library/user-event
@vitejs/plugin-react
test code上にtsxを書くなら必要
逆に言えば、hooksのtestだけなら不要
#WIP
具体例
https://kentcdodds.com/blog/static-vs-unit-vs-integration-vs-e2e-tests#integration
こんな感じでかける
code:ts
import { render, screen } from '@testing-library/react';
import { describe, test, expect } from 'vitest';
import { Hoge } from './PlacesField';
describe('Hoge', () => {
render(<Hoge />);
test('test', () => {
expect(screen.getByText('Test Form'));
});
});
renderすることで、そのComponentが以下のような感じでrenderされている
code:html
<body>
<Hoge/>
</body>
これ全体screenで取得できる感じ
https://www.builder.io/blog/3-react-testing-library-tips
失敗時のロギング
eslint-plugin-testing-library
https://github.com/testing-library/eslint-plugin-testing-library
fineEventより@testing-library/user-eventを使う
docsよみ
Getting Started
Introduction
Guiding Principles
FAQ
Core API
User Actions
Firing Events
Async Methods
Appearance and Disappearance
Considerations for fireEvent()
Using Fake Timers
Advanced
Accessibility
Custom Queries
Debugging
Querying Within Elements
Configuration Options
Frameworks
DOM Testing Library
Introduction
Install
Example
Setup
API
Cheatsheet
React Testing Library
Introduction
Example
Setup
★API
render
render Options
container
baseElement
hydrate
legacyRoot
wrapper
queries
render Result
...queries
container
baseElement
debug
rerender
unmount
asFragment
cleanup
act
renderHook()
renderHook Options
initialProps
wrapper
renderHook Result
result
rerender
Migrate from Enzyme
FAQ
Cheatsheet
Cypress Testing Library
Puppeteer Testing Library
Testcafe Testing Library
Nightwatch Testing Library
WebdriverIO Testing Library
User Interactions
@testing-library/user-eventへ
Ecosystem
bs-jest-dom
jest-native
react-select-event
eslint-plugin-testing-library
eslint-plugin-jest-dom
riot-testing-library
jasmine-dom
query-extensions
rtl-simple-queries
testing-library-selector